package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Daily;
import cn.wolfcode.trip.base.domain.DailyContent;
import cn.wolfcode.trip.base.domain.Strategy;
import cn.wolfcode.trip.base.mapper.DailyContentMapper;
import cn.wolfcode.trip.base.mapper.DailyMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IDailyService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class DailyServiceImpl implements IDailyService {
    @Autowired
    private DailyMapper dailyMapper;
    @Autowired
    private DailyContentMapper contentMapper;



    @Override
    public void save(Daily daily) {
        dailyMapper.insert(daily);
    }


    @Override
    public PageInfo<Daily> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<Daily> list = dailyMapper.selectForList(qo);
        return new PageInfo<>(list);
    }

    @Override
    public void update(Daily daily) {
        dailyMapper.updateByPrimaryKey(daily);
    }

    @Override
    public Daily getDaily(Long dailyId) {
        return  dailyMapper.selectByPrimaryKey(dailyId);
    }

    @Override
    public void saveOrUpdate(Daily daily) {
            if(daily.getState() == Daily.STATE_HOT && daily.getReleaseTime() == null){
                daily.setReleaseTime(new Date());
            }
        DailyContent content = daily.getContent();


        if (daily.getId()!=null){
                //修改

            content.setId(daily.getId());
            contentMapper.updateByPrimaryKey(content);
            dailyMapper.updateByPrimaryKey(daily);
            }else {
                daily.setCreatTime(new Date());
                //保存

            content.setId(daily.getId());
            contentMapper.insert(content);
            dailyMapper.insert(daily);
            }

    }

    @Override
    public Object getContentBydailyId(Long dailyId) {
        return contentMapper.selectByPrimaryKey(dailyId);
    }

    @Override
    public void deleteById(Long id) {
        dailyMapper.deleteByPrimaryKey(id);
        contentMapper.deleteByPrimaryKey(id);

    }

    @Override
    public Object selectDailysByUserId(QueryObject qo, Long id) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<Daily> list = dailyMapper.selectDailysByUserId(id);
        return new PageInfo(list);
    }

}
